﻿using System;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Input;

namespace WickedFlame.GenDock
{
    public class ContentDocument : ContentControl
    {
        //public event EventHandler DockChanged;

        static ContentDocument()
        {
            FocusableProperty.OverrideMetadata(typeof(ContentDocument), new FrameworkPropertyMetadata(true));
        }

        public ContentDocument()
        {
            this.Loaded += new RoutedEventHandler(OnLoaded);
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            //this.MinWidth = this.Width;
        }
        

        DockingManagerService _service;
        internal DockingManagerService ManagerService
        {
            get
            {
                return _service;
            }
        }

        internal void RegisterManagerService(DockingManagerService service)
        {
            _service = service;
        }

        //protected override void OnInitialized(EventArgs e)
        //{
        //    base.OnInitialized(e);

        //    this.CommandBindings.Add(
        //        new CommandBinding(DockablePanel.ToggleAutoHideCommand, this.OnExecuteCommand, this.OnCanExecuteCommand));
        //    this.CommandBindings.Add(
        //        new CommandBinding(HideCommand, this.OnExecuteCommand, this.OnCanExecuteCommand));
        //}

        //static object syncRoot = new object();

        //private static RoutedUICommand hideCommand = null;
        //public static RoutedUICommand HideCommand
        //{
        //    get
        //    {
        //        lock (syncRoot)
        //        {
        //            if (null == hideCommand)
        //            {
        //                hideCommand = new RoutedUICommand("H_ide", "Hide", typeof(ContentDocument));
        //            }
        //        }
        //        return hideCommand;
        //    }
        //}

        //internal virtual void OnExecuteCommand(object sender, ExecutedRoutedEventArgs e)
        //{
        //    //if (!e.Handled &&
        //    //    e.Command == DockablePane.ToggleAutoHideCommand)
        //    //{
        //    //    if ((DockableStyle & DockableStyle.AutoHide) > 0)
        //    //    {
        //    //        ((ContainerPane as DockablePane)).ToggleAutoHide();
        //    //        e.Handled = true;
        //    //    }
        //    //}
        //    //else 
        //        if (!e.Handled && e.Command == ContentDocument.HideCommand)
        //    {
        //        //Manager.Hide(this);

        //        e.Handled = true;
        //    }
        //    //else if (!e.Handled && e.Command == DockableContent.FloatingCommand)
        //    //{
        //    //    Manager.Show(this, DockableContentState.FloatingWindow);
        //    //    e.Handled = true;
        //    //}
        //    //else if (!e.Handled && e.Command == DockableContent.DockableCommand)
        //    //{
        //    //    Manager.Show(this, DockableContentState.DockableWindow);
        //    //    e.Handled = true;
        //    //}
        //    //else if (!e.Handled && e.Command == DockableContent.ShowAsDocumentCommand)
        //    //{
        //    //    Manager.Show(this, DockableContentState.Document);
        //    //    e.Handled = true;
        //    //}

        //}

        //protected virtual void OnCanExecuteCommand(object sender, CanExecuteRoutedEventArgs e)
        //{
        //    //if (State == DockableContentState.AutoHide)
        //    //{
        //    //    if (e.Command == DockingPanel.ToggleAutoHideCommand ||
        //    //        e.Command == DockingPanel.CloseCommand ||
        //    //        e.Command == DockableContent.HideCommand)
        //    //        e.CanExecute = true;
        //    //    else
        //    //        e.CanExecute = false;
        //    //}
        //    //else
        //        e.CanExecute = true;
        //}

        public string Title
        {
            get
            {
                return (string)GetValue(TitleProperty);
            }
            set
            {
                SetValue(TitleProperty, value);
            }
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(ContentDocument), new PropertyMetadata(""));

        public object Icon
        {
            get { return (object)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

        public static readonly DependencyProperty IconProperty = 
            DependencyProperty.Register("Icon", typeof(object), typeof(ContentDocument), 
            new FrameworkPropertyMetadata(null, new CoerceValueCallback(OnCoerce_Icon)));

        private static object OnCoerce_Icon(DependencyObject o, object value)
        {
            if (value is string)
            {
                Uri iconUri;
                // try to resolve given value as an absolute URI
                if (Uri.TryCreate(value as String, UriKind.RelativeOrAbsolute, out iconUri))
                {
                    ImageSource img = new BitmapImage(iconUri);
                    if (null != img)
                    {
                        //GreyableImage icon = (o as ManagedContent).Icon as GreyableImage;
                        //if (null == icon)
                        //    icon = new GreyableImage();
                        Image icon = (o as ContentDocument).Icon as Image;
                        if (null == icon)
                            icon = new Image();

                        icon.Source = img;
                        icon.Stretch = Stretch.None;
                        icon.SnapsToDevicePixels = true;

                        return icon;
                    }
                }
            }
            return value;
        }

        AnchorType _anchorPlace = AnchorType.None;
        public AnchorType AnchorPlace
        {
            get
            {
                return _anchorPlace;
            }
            set
            {
                _anchorPlace = value;
                //if (DockChanged != null)
                //    DockChanged(this, new EventArgs());
            }
        }
    }
}
